import { memo, FC, useRef } from 'react'
import { Modal, Form, DatePicker, message } from 'antd'
import type { FormInstance } from 'antd'
import { EvaluationStateType, Loading, connect, Dispatch } from 'umi'
import moment, { Moment } from 'moment'

interface IProps {
  visible: boolean
  record: any
  hide: () => void
  dispatch: Dispatch
}

interface FormValues {
  [name: string]: any
}
const sendRemind: FC<IProps> = ({ visible, record, hide, dispatch }) => {
  const formRef = useRef<FormInstance>(null)
  const handleOk = () => {
    formRef.current?.submit()
  }
  const handleCancel = () => {
    formRef.current?.resetFields()
    hide()
  }

  // 限制当天之前的日期不可选
  const disabledDate = (current: Moment) => {
    return current && current < moment().subtract(1, 'days') //当天之前的不可选，不包括当天
    //return current && current < moment().endOf("day");当天之前的不可选，包括当天
  }

  const onFinish = (values: FormValues) => {
    const { date } = values
    dispatch({
      type: 'evaluationModel/function_sendWarningSMS',
      payload: {
        unitName: record.name,
        unitCode: record.code,
        date: date.format('YYYY-MM-DD')
      },
      callback: (response) => {
        if (response.code === 200) {
          message.success('发送预警成功')
          handleCancel()
        }
      }
    })
  }
  return (
    <>
      <Modal
        title="发送预警"
        open={visible}
        onOk={handleOk}
        onCancel={handleCancel}
        width="30%"
        okText="确认"
        cancelText="取消"
        maskClosable={false}
      >
        <Form layout={'inline'} onFinish={onFinish} ref={formRef}>
          <Form.Item
            label="资料收集截止日期"
            name="date"
            rules={[{ required: true, message: '请选择资料收集截止日期!' }]}
          >
            <DatePicker
              placeholder="请选择资料收集截止日期"
              disabledDate={disabledDate}
            />
          </Form.Item>
        </Form>
      </Modal>
    </>
  )
}

const mapStateToProps = ({
  evaluationModel,
  loading
}: {
  evaluationModel: EvaluationStateType
  loading: Loading
}) => {
  return {
    evaluationModel,
    evaluationLoding: loading.models.evaluationModel
  }
}

export default connect(mapStateToProps)(memo(sendRemind))
